//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "EURLibor.h"
using namespace Cephei::QL::Indexes::Ibor;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Indexes::Ibor::CEURLibor::CEURLibor (Cephei::QL::Times::IPeriod^ tenor, Cephei::QL::Termstructures::IYieldTermStructure^ h) : CIborIndex(CEURLibor::typeid)
{
    CPeriod^ _Ctenor;
    CYieldTermStructure^ _Ch;
    try
    {
#ifdef HANDLE
        _phEURLibor = NULL;
#endif
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        _Ch = safe_cast<CYieldTermStructure^> (h);
        _Ch->Lock();
        Handle<QuantLib::YieldTermStructure>& _h = static_cast<Handle<QuantLib::YieldTermStructure>&> (_Ch->GetHandle ()); 
        _ppEURLibor = new boost::shared_ptr<QuantLib::EURLibor> (new QuantLib::EURLibor ( _tenor,  _h ));
        SetIborIndex (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppEURLibor));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ch != nullptr) _Ch->Unlock();
    }
}
Cephei::QL::Indexes::Ibor::CEURLibor::CEURLibor (Cephei::QL::Times::IPeriod^ tenor) : CIborIndex(CEURLibor::typeid)
{
    CPeriod^ _Ctenor;
    try
    {
#ifdef HANDLE
        _phEURLibor = NULL;
#endif
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        _ppEURLibor = new boost::shared_ptr<QuantLib::EURLibor> (new QuantLib::EURLibor ( _tenor ));
        SetIborIndex (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppEURLibor));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
    }
}
Cephei::QL::Indexes::Ibor::CEURLibor::CEURLibor (boost::shared_ptr<QuantLib::EURLibor>& childNative, Object^ owner) : CIborIndex(CEURLibor::typeid)
{
#ifdef HANDLE
	_phEURLibor = NULL;
#endif
	_ppEURLibor = &childNative;
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppEURLibor));
}
Cephei::QL::Indexes::Ibor::CEURLibor::CEURLibor (QuantLib::EURLibor& childNative, Object^ owner) : CIborIndex(CEURLibor::typeid)
{
#ifdef HANDLE
	_phEURLibor = NULL;
#endif
	_ppEURLibor = new boost::shared_ptr<QuantLib::EURLibor> (&childNative);
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppEURLibor));
    _EURLiborOwner = owner;
    _IborIndexOwner = owner;
}

Cephei::QL::Indexes::Ibor::CEURLibor::CEURLibor (CEURLibor^ copy) : CIborIndex(CEURLibor::typeid)
{
#ifdef HANDLE
	_phEURLibor = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppEURLibor = new boost::shared_ptr<QuantLib::EURLibor> (copy->GetShared());
        _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppEURLibor));
    }
}
Cephei::QL::Indexes::Ibor::CEURLibor::CEURLibor (System::Type^ t) : CIborIndex(CEURLibor::typeid)
{
#ifdef HANDLE
	_phEURLibor = NULL;
#endif
	if (!t->IsSubclassOf(CEURLibor::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Indexes::Ibor::CEURLibor::CEURLibor (QuantLib::Handle<QuantLib::EURLibor>& childNative, Object^ owner)  : CIborIndex(CEURLibor::typeid)
{
	_phEURLibor = &childNative;
	_ppEURLibor = &static_cast<boost::shared_ptr<QuantLib::EURLibor>>(childNative.currentLink());
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppEURLibor));
    _EURLiborOwner = owner;
}
Cephei::QL::Indexes::Ibor::CEURLibor::CEURLibor (QuantLib::Handle<QuantLib::EURLibor> childNative)  : CIborIndex(CEURLibor::typeid)
{
	_phEURLibor = &childNative;
	_ppEURLibor = &static_cast<boost::shared_ptr<QuantLib::EURLibor>>(childNative.currentLink());
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppEURLibor));
}
#endif
#ifdef STRUCT
Cephei::QL::Indexes::Ibor::CEURLibor::CEURLibor (QuantLib::EURLibor childNative)  : CIborIndex(CEURLibor::typeid)
{
#ifdef HANDLE
	_phEURLibor = NULL;
#endif
	_ppEURLibor = new boost::shared_ptr<QuantLib::EURLibor> (new QuantLib::EURLibor (childNative));
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppEURLibor));
}
#endif

Cephei::QL::Indexes::Ibor::CEURLibor::~CEURLibor ()
{
    if (_ppEURLibor != NULL)
    {
	    delete _ppEURLibor;
        _ppEURLibor = NULL;
    }
}
Cephei::QL::Indexes::Ibor::CEURLibor::!CEURLibor ()
{
    if (_ppEURLibor != NULL)
    {
	    delete _ppEURLibor;
    }
}
QuantLib::EURLibor& Cephei::QL::Indexes::Ibor::CEURLibor::GetReference ()
{
    if (_ppEURLibor == NULL) throw gcnew NativeNullException ();
	return **_ppEURLibor;
}
boost::shared_ptr<QuantLib::EURLibor>& Cephei::QL::Indexes::Ibor::CEURLibor::GetShared ()
{
    if (_ppEURLibor == NULL) throw gcnew NativeNullException ();
	return *_ppEURLibor;
}
QuantLib::EURLibor* Cephei::QL::Indexes::Ibor::CEURLibor::GetPointer ()
{
    if (_ppEURLibor == NULL) throw gcnew NativeNullException ();
	return &**_ppEURLibor;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::EURLibor>& Cephei::QL::Indexes::Ibor::CEURLibor::GetHandle ()
{
	if (_phEURLibor == NULL)
	{
		_phEURLibor = new Handle<QuantLib::EURLibor> (*_ppEURLibor);
	}
	return *_phEURLibor;
}
#endif
bool Cephei::QL::Indexes::Ibor::CEURLibor::HasNative () 
{
	return (_ppEURLibor != NULL);
}

DateTime Cephei::QL::Indexes::Ibor::CEURLibor::MaturityDate (DateTime valueDate)
{
    try
    {
        QuantLib::Date _valueDate = (QuantLib::Date)ValueHelper::Convert (valueDate);
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppEURLibor)->maturityDate ( _valueDate );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Indexes::Ibor::CEURLibor::ValueDate (DateTime fixingDate)
{
    try
    {
        QuantLib::Date _fixingDate = (QuantLib::Date)ValueHelper::Convert (fixingDate);
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppEURLibor)->valueDate ( _fixingDate );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Indexes::Ibor::IEURLibor^ Cephei::QL::Indexes::Ibor::CEURLibor_Factory::Create (Cephei::QL::Times::IPeriod^ tenor, Cephei::QL::Termstructures::IYieldTermStructure^ h)
{
    return gcnew CEURLibor ( tenor,  h);
}
Cephei::QL::Indexes::Ibor::IEURLibor^ Cephei::QL::Indexes::Ibor::CEURLibor_Factory::Create (Cephei::QL::Times::IPeriod^ tenor)
{
    return gcnew CEURLibor ( tenor);
}
